package pseudoAssemblerCompiler;

import pseudoAssemblerCompiler.twopassassembler.common.NotificationCenter;
import pseudoAssemblerCompiler.twopassassembler.controller.AbstractController;
import pseudoAssemblerCompiler.twopassassembler.controller.OperationCodeControllerImpl;
import pseudoAssemblerCompiler.twopassassembler.model.assemblerAlgorithms.FirstAssembler;
import pseudoAssemblerCompiler.twopassassembler.model.assemblerAlgorithms.IViewModel;
import pseudoAssemblerCompiler.twopassassembler.model.codeoperationtable.ICodeOperationTableModel;
import pseudoAssemblerCompiler.twopassassembler.model.io.CodeOperationTableParser;
import pseudoAssemblerCompiler.twopassassembler.model.io.IReader;
import pseudoAssemblerCompiler.twopassassembler.model.io.ReaderImpl;
import pseudoAssemblerCompiler.twopassassembler.model.referencetable.ReferenceTableImpl;
import pseudoAssemblerCompiler.twopassassembler.model.symbolnametable.SymbolicNamesTableImpl;
import pseudoAssemblerCompiler.twopassassembler.view.firstpass.codeoperationtable.CodeOperationTableImpl;
import pseudoAssemblerCompiler.twopassassembler.view.TPAView;
import pseudoAssemblerCompiler.twopassassembler.view.WorkSpace;
import pseudoAssemblerCompiler.twopassassembler.view.firstpass.Console;
import pseudoAssemblerCompiler.twopassassembler.view.firstpass.Editor;
import pseudoAssemblerCompiler.twopassassembler.view.secondpass.ReferenceTable;
import pseudoAssemblerCompiler.twopassassembler.view.secondpass.SecondPass;
import pseudoAssemblerCompiler.twopassassembler.view.secondpass.SymbolicNamesTable;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 13.04.12
 * Time: 22:32
 * To change this template use File | Settings | File Templates.
 */
public class PseudoAssemblerCompiler {

    /**
     * Стартовый метод программы
     *
     * @param aConsole консольный ввод
     */
    public static void main(String[] aConsole) {
        AbstractController controller = new OperationCodeControllerImpl();

        IReader reader = new ReaderImpl();
        CodeOperationTableParser parser = new CodeOperationTableParser();
        try {
            reader.readFile("codeoperationtable.txt", parser);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ICodeOperationTableModel codeOperationTable = new pseudoAssemblerCompiler.twopassassembler.model.codeoperationtable.CodeOperationTableImpl();
        codeOperationTable.setCodeOperationTable(parser.getCodeOperationTable());

        ReferenceTableImpl referenceTable = new ReferenceTableImpl();
        controller.addModel(referenceTable);

        SymbolicNamesTableImpl symbolicNameTable = new SymbolicNamesTableImpl();
        controller.addModel(symbolicNameTable);

        FirstAssembler model = new FirstAssembler(codeOperationTable, symbolicNameTable, referenceTable);
        NotificationCenter.getDefaultCenter().addObserver(controller);
//                ((IViewModel) model).addObserver(controller);
        controller.addModel(model);

        controller.addModel((IViewModel) codeOperationTable);

        TPAView view = new TPAView(null);
        view.init();
        view.enableFirstPassButton(false);
        view.enableSecondPassButton(false);
        controller.addMainView(view);
        WorkSpace workSpace = new WorkSpace(view.getComposite());
        workSpace.show();

        SecondPass secondPass = new SecondPass(workSpace.get_secondPassComposite());
        ReferenceTable referenceTableView = new ReferenceTable(secondPass.get_referenceTableComposite());
        SymbolicNamesTable symbolicNamesTable = new SymbolicNamesTable(secondPass.get_symbolicNamesTableComposite());
        controller.addView(referenceTableView);
        controller.addView(symbolicNamesTable);
        secondPass.show();
        referenceTableView.show();
        symbolicNamesTable.show();

        Console console = new Console(workSpace.get_consoleOutputComposite());
        controller.addView(console);
        Editor editor = new Editor(workSpace.get_editorComposite());
        controller.addView(editor);
        CodeOperationTableImpl operationTable = new CodeOperationTableImpl(workSpace.get_differentTableComposite());
        controller.addView(operationTable);
        operationTable.setController(controller);
        console.show();
        editor.show();
        operationTable.show();

        view.setController(controller);


        view.createView();
    }

}
